package com.searchengine.controller;

import com.searchengine.entity.PaymentRecord;
import com.searchengine.entity.User;
import com.searchengine.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 支付控制器
 */
@RestController
@RequestMapping("/api/payments")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    /**
     * 创建支付订单
     */
    @PostMapping("/orders")
    public ResponseEntity<PaymentRecord> createPaymentOrder(
            @RequestAttribute("user") User user,
            @RequestBody Map<String, Object> requestData) {
        String keyword = (String) requestData.get("keyword");
        BigDecimal weight = new BigDecimal(requestData.get("weight").toString());
        Integer durationDays = Integer.parseInt(requestData.get("durationDays").toString());
        
        PaymentRecord order = paymentService.createPaymentOrder(user.getId(), keyword, weight, durationDays);
        return ResponseEntity.ok(order);
    }

    /**
     * 生成支付URL
     */
    @GetMapping("/pay-url")
    public ResponseEntity<String> generatePaymentUrl(
            @RequestParam String orderNo,
            @RequestParam BigDecimal amount,
            @RequestParam String subject) {
        String paymentUrl = paymentService.generatePaymentUrl(orderNo, amount, subject);
        return ResponseEntity.ok(paymentUrl);
    }

    /**
     * 处理支付回调
     */
    @PostMapping("/callback")
    public ResponseEntity<String> handlePaymentCallback(@RequestParam Map<String, String> params) {
        boolean success = paymentService.handlePaymentCallback(params);
        if (success) {
            return ResponseEntity.ok("success");
        } else {
            return ResponseEntity.ok("fail");
        }
    }

    /**
     * 查询订单状态
     */
    @GetMapping("/orders/{orderNo}")
    public ResponseEntity<PaymentRecord> queryOrderStatus(@PathVariable String orderNo) {
        PaymentRecord order = paymentService.queryOrderStatus(orderNo);
        return ResponseEntity.ok(order);
    }

    /**
     * 关闭订单
     */
    @PutMapping("/orders/{orderNo}/close")
    public ResponseEntity<Boolean> closeOrder(@PathVariable String orderNo) {
        boolean success = paymentService.closeOrder(orderNo);
        return ResponseEntity.ok(success);
    }

    /**
     * 获取用户的支付记录
     */
    @GetMapping("/orders/user")
    public ResponseEntity<List<PaymentRecord>> getUserPaymentRecords(@RequestAttribute("user") User user) {
        List<PaymentRecord> records = paymentService.getUserPaymentRecords(user.getId());
        return ResponseEntity.ok(records);
    }

    /**
     * 计算支付金额
     */
    @GetMapping("/calculate-amount")
    public ResponseEntity<BigDecimal> calculateAmount(
            @RequestParam BigDecimal weight,
            @RequestParam Integer durationDays) {
        BigDecimal amount = paymentService.calculateAmount(weight, durationDays);
        return ResponseEntity.ok(amount);
    }
}